﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Areas.Admin.Controllers
{
    public class AboutController : Controller
    {
        private readonly Manager.BLL.Admin.M_AboutBll bll = new Manager.BLL.Admin.M_AboutBll();
        //
        // GET: /Admin/About/

        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.AboutSet, Ope = "view")]
        public ActionResult Index()
        {
            var query = from a in bll.GetModelList("")
                        select new Models.About
                        {
                            AboutID = a.AboutID,
                            AboutTitle = a.AboutTitle,
                            AboutContent = a.AboutContent,
                            AboutAuthor = a.AboutAuthor,
                            AboutTypeName = new Manager.BLL.Admin.M_CategoryBll().GetModel(a.AboutTypeID.Value).TypeName,
                            AboutDate = a.AboutDate,
                            AboutSource = a.AboutSource,
                            IsValid=a.IsValid?"是":"否",
                            IsTop = a.IsTop ? "是" : "否"
                        };
            return View(query.ToList<Models.About>());
        }

        [HttpPost]
        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.AboutSet, Ope = "view")]
        public ActionResult Index(FormCollection forms)
        {
            try
            {
                string AboutID = forms["tbxSearchAboutID"];
                string AboutTitle = forms["tbxSearchAboutTitle"];
                string AboutAuthor = forms["tbxSearchAboutAuthor"];
                string AboutSource = forms["tbxSearchAboutSource"];
                string AboutTypeName = forms["tbxSearchAboutTypeName"];
                string AboutIsTop = forms["tbxSearchIsTop"];
                string AboutBeginDate = forms["tbxSearchAboutBeginDate"];
                string AboutEndDate = forms["tbxSearchAboutEndDate"];

                var query = from a in bll.GetModelList("")
                            select new Models.About
                            {
                                AboutID = a.AboutID,
                                AboutTitle = a.AboutTitle,
                                AboutContent = a.AboutContent,
                                AboutAuthor = a.AboutAuthor,
                                AboutTypeName = new Manager.BLL.Admin.M_CategoryBll().GetModel(a.AboutTypeID.Value).TypeName,
                                AboutDate = a.AboutDate,
                                AboutSource = a.AboutSource,
                                IsValid = a.IsValid ? "是" : "否",
                                IsTop = a.IsTop ? "是" : "否"
                            };
                if (AboutID.Length > 0)
                    query = query.Where(a => a.AboutID == int.Parse(AboutID));
                if (AboutTitle.Length > 0)
                    query = query.Where(a => a.AboutTitle.Contains(AboutTitle));
                if (AboutAuthor.Length > 0)
                    query = query.Where(a => a.AboutAuthor.Contains(AboutAuthor));
                if (AboutSource.Length > 0)
                    query = query.Where(a => a.AboutSource.Contains(AboutSource));
                if (AboutTypeName.Length > 0)
                    query = query.Where(a => a.AboutTypeName.Contains(AboutTypeName));
                if (AboutIsTop.Length > 0)
                    query = query.Where(a => a.IsTop.Equals(AboutIsTop));
                if (AboutBeginDate.Length > 0)
                    query = query.Where(a => a.AboutDate >= DateTime.Parse(AboutBeginDate));
                if (AboutEndDate.Length > 0)
                    query = query.Where(a => a.AboutDate <= DateTime.Parse(AboutEndDate));
                return View(query.ToList<Models.About>());
            }
            catch
            {
                var query = from a in bll.GetModelList("")
                            select new Models.About
                            {
                                AboutID = a.AboutID,
                                AboutTitle = a.AboutTitle,
                                AboutContent = a.AboutContent,
                                AboutAuthor = a.AboutAuthor,
                                AboutTypeName = new Manager.BLL.Admin.M_CategoryBll().GetModel(a.AboutTypeID.Value).TypeName,
                                AboutDate = a.AboutDate,
                                AboutSource = a.AboutSource,
                                IsValid = a.IsValid ? "是" : "否",
                                IsTop = a.IsTop ? "是" : "否"
                            };
                return View(query.ToList<Models.About>());
            }
        }

        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.AboutSet, Ope = "add")]
        public ActionResult Add()
        {
            BindSelect();
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.AboutSet, Ope = "add")]
        public ActionResult Add(Manager.Model.Admin.M_About model)
        {
            Manager.BLL.Admin.M_CategoryBll categoryBll = new Manager.BLL.Admin.M_CategoryBll();

            try
            {
                model.AboutAuthor = (Session["administrator"] as Manager.Model.Admin.M_Admin).Realname;
                if (!categoryBll.Exists(model.AboutTypeID.Value))
                    ModelState.AddModelError("", Resources.Global.msg_select_exist);

                //上传
                if (Request.Files.Count > 0)
                {
                    HttpPostedFileBase fileImage = Request.Files[0];
                    if (fileImage.FileName.Length != 0)
                    {
                        string urlImage = (new Manager.Tools.FileUpload()).SaveFile(fileImage, "About");
                        if (urlImage.IndexOf("/") == -1)
                        {
                            ModelState.AddModelError("AboutImage", urlImage);
                        }
                        else
                        {
                            model.AboutImage = urlImage;
                        }
                    }
                }

                if (ModelState.IsValid)
                {
                    int r = bll.Add(model);
                    if (r > 0)
                        return RedirectToAction("Index", "About");
                    else
                        ModelState.AddModelError("", Resources.Global.msg_error);
                }
            }
            catch
            {
                ModelState.AddModelError("", Resources.Global.msg_error);
            }
            BindSelect();
            return View(model);
        }

        /// <summary>
        /// 栏目类别下拉列表框
        /// </summary>
        public void BindSelect()
        {
            Manager.BLL.Admin.M_CategoryBll categoryBll = new Manager.BLL.Admin.M_CategoryBll();
            IEnumerable<SelectListItem> categorys = from c in categoryBll.GetModelList("ModuleID=" + ((int)Manager.Tools.AuthPermission.ModuleTypes.AboutSet).ToString()) select new SelectListItem { Value = c.TypeID.ToString(), Text = c.TypeName };
            ViewBag.CategorySelect = categorys;
        }

        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.AboutSet, Ope = "edit")]
        public ActionResult Edit(int id)
        {
            if (!bll.Exists(id))
                return RedirectToAction("Index");
            BindSelect();
            return View(bll.GetModel(id));
        }

        [HttpPost]
        [ValidateInput(false)]
        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.AboutSet, Ope = "edit")]
        public ActionResult Edit(int id, Manager.Model.Admin.M_About model)
        {
            Manager.BLL.Admin.M_CategoryBll categoryBll = new Manager.BLL.Admin.M_CategoryBll();
            model.AboutID = id;
            if (!bll.Exists(id))
                ModelState.AddModelError("", Resources.Global.msg_select_exist);
            try
            {
                //上传
                if (Request.Files.Count > 0)
                {
                    HttpPostedFileBase fileImage = Request.Files[0];
                    if (fileImage.FileName.Length != 0)
                    {
                        bool res = (new Manager.Tools.FileUpload()).DeleteExistFile(Server.MapPath("~" + model.AboutImage));
                        string urlImage = (new Manager.Tools.FileUpload()).SaveFile(fileImage, "About");
                        if (urlImage.IndexOf("/") == -1)
                        {
                            ModelState.AddModelError("AboutImage", urlImage);
                        }
                        else
                        {
                            model.AboutImage = urlImage;
                        }
                    }
                }

                if (ModelState.IsValid)
                {
                    if (!categoryBll.Exists(model.AboutTypeID.Value))
                        ModelState.AddModelError("", Resources.Global.msg_select_exist);


                    model.AboutAuthor = (Session["administrator"] as Manager.Model.Admin.M_Admin).Realname;
                    bool r = bll.Update(model);
                    if (r)
                        return RedirectToAction("Index", "About");
                    else
                        ModelState.AddModelError("", Resources.Global.msg_error);
                }
            }
            catch
            {
                ModelState.AddModelError("", Resources.Global.msg_error);
            }
            BindSelect();
            return View(model);
        }

        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.AboutSet, Ope = "del")]
        public ActionResult Del(int id)
        {
            if (!bll.Exists(id))
                return Content("<script>alert('" + Resources.Global.msg_select_exist + "');location.href='/Admin/About/Index';</script>");
            try
            {
                bool r = bll.Delete(id);
                if (r)
                    return Content("<script>alert('" + Resources.Global.msg_success + "');location.href='/Admin/About/Index';</script>");
                else
                    return Content("<script>alert('" + Resources.Global.msg_error + "');location.href='/Admin/About/Index';</script>");

            }
            catch (Exception)
            {
                return JavaScript("<script>alert('" + Resources.Global.msg_error + "');location.href='/Admin/About/Index';</script>");
            }
        }
    }
}
